package com.demo.conf;

import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.web.ErrorPageFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.demo.interpector.LoginInterceptor;
import com.demo.interpector.MobileInterceptor;

@Configuration
@EnableWebMvc
public class WebMVCConfiguration extends WebMvcConfigurerAdapter{
	
	 @Override
     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
     }
	 
	 @Override
	 public void addResourceHandlers(ResourceHandlerRegistry registry) {
		 registry.addResourceHandler("/**")
      			.addResourceLocations("classpath:/public/");
		 
	     registry.addResourceHandler("/static/**")
	             .addResourceLocations("classpath:/static/");
	     
	     registry.addResourceHandler("/h5/**")
	     .addResourceLocations("classpath:/h5/");
	 }
	 
	 @Bean
     public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".ftl");
        return resolver;
     }
	 
	 @Bean
	 public ErrorPageFilter errorPageFilter() {
	     return new ErrorPageFilter();
	 }

	 @Bean
	 public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
	     FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
	     filterRegistrationBean.setFilter(filter);
	     filterRegistrationBean.setEnabled(false);
	     return filterRegistrationBean;
	 }
	 
	 @Override
	 public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("index");
		registry.addViewController("/login").setViewName("login");
		
//			registry.addViewController("/admin/profile").setViewName("admin/profile");		
//			registry.addViewController("/hello").setViewName("hello");
	 }
	 
	 
	//增加过滤器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//先进入登录过滤
		registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
		//手机站过滤
		registry.addInterceptor(new MobileInterceptor()).addPathPatterns("/**");
	}
	 
//	@Bean
//	public FreeMarkerConfigurer freeMarkerConfigurer() {
//	    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
//	    configurer.setPreTemplateLoaders(templateService);
//	    return configurer;
//	}
	 
	 

}
